package listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import panelSites.SendedEmailPanel;
import persistence.Serializer;
import propertiesReader.PropertiesReader;
import utils.CreateImage;
import xls.ReadXLS;
import fileIO.ReadTxtFiles;

public class EmailStatusListener implements ActionListener {

	private SendedEmailPanel view;
	@SuppressWarnings("rawtypes")
	private JList table;
	private JScrollPane scrollit;
	private PropertiesReader pReader;
	

	public EmailStatusListener(SendedEmailPanel sendedEmailPanel,
			boolean endversion) {
		view = sendedEmailPanel;
		pReader = new PropertiesReader(endversion);
	}

	public void actionPerformed(ActionEvent e) {
		JButton sourceButton = (JButton) e.getSource();
		Icon icon = CreateImage.createImageIcon("arrow_up.png");
		view.backButton.setIcon(icon);
		view.getInfoPanel().removeAll();
		if (sourceButton.equals(view.getShow())) {
			goAction();
		} else if (sourceButton.equals(view.getDel())) {
			delAction();
		} else if (sourceButton.equals(view.backButton)) {
			backAction();
		} else if (sourceButton.equals(view.getToSend())) {
				showImportedAction();
		} else if (sourceButton.equals(view.getDelToSend())) {
			showWrongEmailAdresses();
		}
		view.repaint();
		view.setVisible(true);
	}

	@SuppressWarnings({ "unchecked", "rawtypes", "unused" })
	private void showWrongEmailAdresses() {
		Set<String> emails = loadAndReturnSendedEmailList("serializedWrongEmails");
		List<String> blackList = new LinkedList<String>();
		ReadTxtFiles readtxt = new ReadTxtFiles();
		try {
			blackList = readtxt.readTxtFileReturnLines(pReader.getProperty("blacklistPath"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		emails.addAll(blackList);
		if (emails != null) {
			table = new JList(emails.toArray());
			table.setVisibleRowCount(10);
			scrollit = new JScrollPane(table);

			view.getInfoPanel().add(scrollit);

		} else {
			JLabel notfound = new JLabel("Keine Angaben gefunden. ");
			view.getInfoPanel().add(notfound);
		}

		view.validate();
		view.repaint();

	}

	@SuppressWarnings({ "rawtypes", "unused", "unchecked" })
	private void showImportedAction() {
		String xlsfile = pReader.getProperty("xlsPath");
		ReadXLS xlsReader = ReadXLS.getInstance();
		Set<String> emails = xlsReader.readXLS(xlsfile);
		ReadTxtFiles readtxt = new ReadTxtFiles();
		List<String> blackList = new LinkedList<String>();
		try {
			blackList = readtxt.readTxtFileReturnLines(pReader.getProperty("blacklistPath"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		emails.removeAll(blackList);
		if (emails != null) {
			table = new JList(emails.toArray());
			table.setVisibleRowCount(10);
			scrollit = new JScrollPane(table);

			view.getInfoPanel().add(scrollit);

		} else {
			JLabel notfound = new JLabel("Keine Angaben gefunden. ");
			view.getInfoPanel().add(notfound);
		}

		view.validate();
		view.repaint();


	}

	private void backAction() {
		JPanel fillPanel = new JPanel();
		fillPanel.setBackground(Color.DARK_GRAY);
		view.getInfoPanel().add(fillPanel);
		view.getInfoPanel().setBackground(Color.DARK_GRAY);
		Icon icon = CreateImage.createImageIcon("picture_empty.png");
		view.backButton.setIcon(icon);
	}

	private void delAction() {
		Serializer serializer = new Serializer();
		serializer.serialize(
				pReader.getProperty("serializedEmailsPath"),
				new HashSet<String>());
		serializer.serialize(
				pReader.getProperty("serializedWrongEmails"),
				new HashSet<String>());
		JLabel notfound = new JLabel("Emails gel�scht.");
		notfound.setBackground(Color.WHITE);
		view.getInfoPanel().setBackground(Color.WHITE);
		view.getInfoPanel().add(notfound);
		view.validate();
		view.repaint();
	}

	private Set<String> loadAndReturnSendedEmailList(String fileName) {
		Serializer serializer = new Serializer();
		Set<String> sendedEmailsLoad = serializer.deserialize(pReader
				.getProperty(fileName));
		return sendedEmailsLoad;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void goAction() {
		Set<String> emails = loadAndReturnSendedEmailList("serializedEmailsPath");

		if (emails != null) {
			table = new JList(emails.toArray());
			table.setVisibleRowCount(10);
			scrollit = new JScrollPane(table);

			view.getInfoPanel().add(scrollit);

		} else {
			JLabel notfound = new JLabel("Keine Angaben gefunden. ");
			view.getInfoPanel().add(notfound);
		}

		view.validate();
		view.repaint();

	}

}
